package com.java.comprehensive.arithmetic.leecode.medium;

import cn.hutool.core.util.ArrayUtil;
import com.java.comprehensive.other.sort.People;

import java.util.*;
import java.util.stream.Collectors;

public class LeetCode_347 {

    public int[] topKFrequent1(int[] nums, int k) {
        int[] results = new int[k];
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }


        //Comparator 内部函数式变成实现这个接口，比较了数组中 下标1的位置，
        PriorityQueue<int[]> priorityQueue = new PriorityQueue<int[]>((pair1, pair2) -> pair2[1] - pair1[1]);

        map.forEach((key, v) -> {
            priorityQueue.add(new int[]{key, v});
        });
        for (int i = 0; i < k; i++) {
            results[i] = Objects.requireNonNull(priorityQueue.poll())[0];
        }
        return results;
    }

    public static void main(String[] args) {
        LeetCode_347 leetCode_347 = new LeetCode_347();
        int nums[] = {1, 1, 1, 2, 2, 3};
        leetCode_347.topKFrequent1(nums, 2);
        Comparator<People> peopleComparator = new Comparator<People>() {

            //排序每次去集合中第一个和第二个比 所以o1-o2是正序 o2-o1是倒叙
            @Override
            public int compare(People o1, People o2) {
                if (o1.getAge() > o2.getAge()) {
                    return 1;
                } else if (o1.getAge() < o2.getAge()) {
                    return -1;
                }
                return 0;
            }

            @Override
            public boolean equals(Object obj) {
                return false;
            }
        };
        LinkedList<People> people = new LinkedList<>();
        People p = new People();
        p.setName("张三");
        p.setAge(15);
        People p1 = new People();
        p1.setName("李四");
        p1.setAge(18);
        People p2 = new People();
        p2.setName("王五");
        p2.setAge(20);
        People p3 = new People();
        p3.setName("赵六");
        p3.setAge(10);
        people.add(p);
        people.add(p1);
        people.add(p2);
        people.add(p3);
        List<People> collect = people.stream().sorted(peopleComparator).collect(Collectors.toList());
        while (!people.isEmpty() && people.peek() != null) {
            System.out.println(people.poll());
        }
        System.out.println(ArrayUtil.toString(collect));

    }
}
